import cv2
import queue
import time
import threading

# q=queue.Queue()

# user, pwd, ip, channel = "admin", "admin123", "192.168.102.65", 1
# input_st= "rtsp://%s:%s@%s/cam/realmonitor?channel=%d&subtype=0" % (user, pwd, ip, channel)

input_stream_01 = "rtsp://VA:VA1234@10.31.97.1/onvif-media/media.amp" \
                  "?profile=profile_1_h264&sessiontimeout=60&streamtype=unicast"
# input_st_174="rtsp://admin:fudian123@192.168.102.174:554/cam/realmonitor?charnel=2&subtype=0&unicast=true&proto=Onvif"
# input_st_65="rtsp://admin:admin123@192.168.102.65:554/cam/realmonitor?channel=1&subtype=1"
# input_st_66="rtsp://admin:admin123@192.168.102.66:554/cam/realmonitor?channel=1&subtype=1"

cap_01 = cv2.VideoCapture(input_stream_01)

fourcc = cv2.VideoWriter_fourcc(*'mp4v')

width_01 = int(cap_01.get(cv2.CAP_PROP_FRAME_WIDTH))
height_01 = int(cap_01.get(cv2.CAP_PROP_FRAME_HEIGHT))

print(cap_01.get(cv2.CAP_PROP_FPS))

cap_01.release()

out_01 = cv2.VideoWriter('test.mp4', fourcc, 25.0, (width_01, height_01))

frame_num = 100


def Receive():
    print("start Reveive")

    cap_01 = cv2.VideoCapture(input_stream_01)

    frame_count = 0
    while True:
        if frame_count == frame_num:
            break

        _, frame_01 = cap_01.read()

        out_01.write(frame_01)
        frame_count += 1
    cap_01.release()
    out_01.release()


# def Display():
#     print("Start Displaying")
#     count = 0
#     activate = True
#     while activate:
#         if q.empty() != True:
#             count += 1
#             print("capture --> %s" % count)
#             if count < num:
#                 activate = True
#                 frame = q.get()
#                 cv2.imshow("frame1", frame)
#                 out.write(frame)
#
#             else:
#                 activate = False
#                 print("Video capture completed!")
#                 cap.release()
#                 out.release()
#                 cv2.destroyAllWindows()
#
#         if cv2.waitKey(1) & 0xFF == ord('q'):
#             break


if __name__ == '__main__':
    Receive()
    # p1=threading.Thread(target=Receive)
    # p2 = threading.Thread(target=Display)
    # p1.start()
    # p2.start()
